import {baseUrl, debugMode, imgPrefixUrl} from "../myConfig";

function checkType(any) {
  return Object.prototype.toString.call(any).slice(8, -1)
}
function clone(any){
  if(checkType(any) === 'Object') { // 拷贝对象
    let o = {};
    for(let key in any) {
      o[key] = clone(any[key])
    }
    return o;
  } else if(checkType(any) === 'Array') { // 拷贝数组
    var arr = []
    for(let i = 0,leng = any.length;i<leng;i++) {
      arr[i] = clone(any[i])
    }
    return arr;
  } else if(checkType(any) === 'Function') { // 拷贝函数
    return new Function('return '+any.toString()).call(this)
  } else if(checkType(any) === 'Date') { // 拷贝日期
    return new Date(any.valueOf())
  } else if(checkType(any) === 'RegExp') { // 拷贝正则
    return new RegExp(any)
  } else if(checkType(any) === 'Map') { // 拷贝Map 集合
    let m = new Map()
    any.forEach((v,k)=>{
      m.set(k, clone(v))
    })
    return m
  } else if(checkType(any) === 'Set') { // 拷贝Set 集合
    let s = new Set()
    for(let val of any.values()) {
      s.add(clone(val))
    }
    return s
  }
  return any;
}

export function copyObj(src){
  return clone(src)
}

export function simpleCopy(src,dst){
  let keys = Object.keys(dst)
  for (const key of keys) {
    dst[key] = src[key]
  }
}
export function simpleCopyBySrc(src,dst){
  let keys = Object.keys(src)
  for (const key of keys) {
    dst[key] = src[key]
  }
}

export function myNumberFormat(i){
  if (!i){
    return '0';
  }
  if (i>=1e8){
    return (i/1e8).toFixed(1)+'亿';
  }else if (i>=1e4){
    return (i/1e4).toFixed(1)+'万';
  }else{
    return i;
  }
}

export function myDateFormat(i){
  let str = i.split(' ')
  let now = new Date();
  let ii = new Date(i);
  if (now.getFullYear() === ii.getFullYear()+1){
    return '去年 '+i.substring(5,19)
  }else if (now.getDate() === ii.getDate()){
    return '今天 '+str[1]
  }else if (now.getDate() === ii.getDate()+1){
    return '昨天 '+str[1]
  }else{
    return i
  }
}

export const rules = {
  required: value => !!value || '不能为空哦',
  phone: val => {
    const pattern = /^1[3-9][0-9]{9}$/
    // return pattern.test(val) || '手机号格式不对哦'},
    return pattern.test(val) || '手机号格式不对哦'},
  email: value => {
    const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return pattern.test(value) || '邮箱格式不对哦'
  },
  verifyCode: value => {
    const pattern = /^\d{6}$/
    return pattern.test(value) || '验证码格式不对哦'
  },
  password: value => {
    const pattern = /^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,12}$/
    return pattern.test(value) || '密码格式不对哦'
  },
  maxLength: length =>{
    return value => value.length <= length || '超出长度了哦'
  }
}

export function printMsg(msg,context,tip){
  let name = 'logging'
  if (context) {
    name = context.$options.name
  }
  if (debugMode){
    console.log(msg)
    console.log(`=====^ ${tip?tip+',':""} ${name} ^=====`)
  }
}

export function utf8_to_b64( str ) {
  return window.btoa(unescape(encodeURIComponent( str )));
}

export function b64_to_utf8( str ) {
  return decodeURIComponent(escape(window.atob( str )));
}

export function getImg(img){
  if (img){
    return baseUrl+imgPrefixUrl+img
  }else{
    return ''
  }
}
